"use client";
import Title from "antd/es/typography/Title";
import { message } from "antd";
import QuestionTable from "@/components/QuestionTable";
import "./index.css";
import { getQuestionSimpleVoByPageUsingPost } from "@/api/questionController";
import { useParams } from "next/navigation";
import { useEffect } from "react";

/**
 * 题目列表页面
 * @constructor
 */
export default function QuestionsPage() {
  const searchParams = useParams(); // 获取路由参数
  // 获取 url 的查询参数
  const { q: searchText } = searchParams;
  // 题目列表和总数
  let questionList: API.QuestionVO[] = [];
  let total = 0;

  useEffect(() => {
    const fetchData = async () => {
      try {
        const res = await getQuestionSimpleVoByPageUsingPost({
          searchText,
          pageSize: 12,
          sortField: "createTime",
          sortOrder: "desc",
        } as any);
        questionList = (res.data.records as API.QuestionVO[]) ?? [];
      } catch (e: any) {
        message.error("获取题目列表失败，" + e.message);
      }
    };
  });
  return (
    <div id="questionsPage" className="max-width-content">
      <Title level={3}>题目大全</Title>
      <QuestionTable
        defaultQuestionList={questionList}
        defaultTotal={total}
        defaultSearchParams={{
          title: searchText,
        }}
      />
    </div>
  );
}
